package com.zzg.pattern.builder.inter.impl;

import com.zzg.pattern.builder.entity.User;
import com.zzg.pattern.builder.inter.Builder;

/**
 * 
 * @ClassName:  UserBuilder 建造模式  
 * @Description:所有的属性都设置给 Builder，然后 getBuilder() 方法的时候，将这些属性复制给实际产生的对象(User)。
 * @author: 世纪伟图 -zzg
 * @date:   2019年2月12日 上午9:56:16   
 *     
 * @Copyright: 2019 www.digipower.cn 
 * 注意：本内容仅限于深圳市世纪伟图科技开发有限公司内部使用，禁止用于其他的商业目的
 */
public class UserBuilder implements Builder<User> {
	// 实体属性
	private String name;
	private int age;
	private String address;

	// set 和 get
	public UserBuilder name(String name) {
		this.name = name;
		return this;
	}

	
	public UserBuilder age(int age) {
		this.age = age;
		return this;
	}

	
	public UserBuilder address(String address) {
		this.address = address;
		return this;
	}
	
	// 构造函数
	public UserBuilder(){
		super();
	}

	
	@Override
	public User getBuilder() {
		// TODO Auto-generated method stub
		if (name == null || address == null) {
            throw new RuntimeException("用户名和地址必填");
        }
        if (age <= 0 || age >= 150) {
            throw new RuntimeException("年龄不合法");
        }
		return new User(name, age, address);
	}
	
	public static void main(String[] args){
		User user = new UserBuilder().name("zzg").age(28).address("中国.深圳").getBuilder();
		System.out.println("user name is:" + user.getName());
		System.out.println("user age is:" + user.getAge());
		System.out.println("user address is:" + user.getAddress());
	}

}
